package com.job.search.listener;

import cn.hutool.core.text.replacer.StrReplacer;
import com.alibaba.fastjson2.JSON;
import com.job.client.common.result.ResponseResult;
import com.job.client.common.utils.DateTimeUtils;
import com.job.client.model.company.dto.CompanyEsDto;
import com.job.client.model.company.pojo.Company;
import com.job.client.model.posts.dto.PostsEsDto;
import com.job.client.model.posts.pojo.Posts;
import com.job.client.model.rabbitmq.MysqlToEsGDSNConstant;
import com.job.search.service.SearchCompanyService;
import com.job.search.service.SearchPostsService;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.pqc.crypto.newhope.NHSecretKeyProcessor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GDSNListener {
    @Autowired
    private SearchPostsService searchPostsService;
    @Autowired
    private SearchCompanyService searchCompanyService;

    @RabbitListener(queues = MysqlToEsGDSNConstant.QUEUE_GDSN_POSTS)
    public void updatePosts(String jsonData){
        System.out.println("posts");
        if(jsonData == null){
            log.error("es与mysql数据同步的职位队列获得数据失败");
            return;
        }
        Posts posts = JSON.parseObject(jsonData, Posts.class);
        PostsEsDto postsEsDto  = new PostsEsDto();
        BeanUtils.copyProperties(posts,postsEsDto);
        postsEsDto.setCreateTime(DateTimeUtils.conversion(posts.getCreateTime()));
        postsEsDto.setUpdateTime(DateTimeUtils.conversion(posts.getUpdateTime()));
        ResponseResult result = searchPostsService.addPosts(postsEsDto);
        if(result.getCode() != 200){
            log.error("MYSQL和ES数据同步职位失败");
            return;
        }
    }

    @RabbitListener(queues =  MysqlToEsGDSNConstant.QUEUE_GDSN_COMPANY)
    public void updateCompany(String jsonData){
        System.out.println("company");
        if(jsonData == null){
            log.error("es与mysql数据同步的公司队列获得数据失败");
            return;
        }
        Company company = JSON.parseObject(jsonData, Company.class);
        CompanyEsDto companyEsDto = new CompanyEsDto();
        BeanUtils.copyProperties(company,companyEsDto);
        companyEsDto.setCreateTime(DateTimeUtils.conversion(company.getCreateTime()));
        companyEsDto.setUpdateTime(DateTimeUtils.conversion(company.getUpdateTime()));
        ResponseResult result = searchCompanyService.addCompany(companyEsDto);
        if(result.getCode() != 200){
            log.error("MYSQL和ES数据同步公司失败");
            return;
        }
    }
}
